From bb2ca3b94d53c009c78d0f364c41ae0220ea2c9e Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Mon, 16 May 2016 10:20:10 -0400 Subject: [PATCH] wayland: fix error handling for memfd_create We currently use syscall() directly to invoke memfd_create, since the function isn't available in libc headers yet. The code, though, mishandles how errors are passed from syscall(). It assumes syscall returns the error code directly (but negative), when in fact, syscall() uses errno. Also, the code fails to retry on EINTR. This commit moves the handling of memfd create to a helper function, and changes the code to use errno and handle EINTR. https://bugzilla.gnome.org/show_bug.cgi?id=766341 --- gdk/wayland/gdkdisplay-wayland.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/gdk/wayland/gdkdisplay-wayland.c b/gdk/wayland/gdkdisplay-wayland.c index c541a6a0b8..9f78f29802 100644 --- a/gdk/wayland/gdkdisplay-wayland.c +++ b/gdk/wayland/gdkdisplay-wayland.c @@ -1104,6 +1104,23 @@ typedef struct _GdkWaylandCairoSurfaceData { uint32_t scale; } GdkWaylandCairoSurfaceData; +static int +open_shared_memory (void) +{ + int ret; + + do + { + ret = syscall (__NR_memfd_create, "gdk-wayland", MFD_CLOEXEC); + } + while (ret < 0 && errno == EINTR); + + if (ret < 0) + g_critical (G_STRLOC ": creating shared memory file failed: %m"); + + return ret; +} + static struct wl_shm_pool * create_shm_pool (struct wl_shm *shm, int size, @@ -1111,19 +1128,13 @@ create_shm_pool (struct wl_shm *shm, void **data_out) { struct wl_shm_pool *pool; - int ret, fd; + int fd; void *data; - ret = syscall (__NR_memfd_create, "gdk-wayland", MFD_CLOEXEC); - - if (ret < 0) - { - g_critical (G_STRLOC ": creating shared memory file failed: %s", - g_strerror (-ret)); - return NULL; - } + fd = open_shared_memory (); - fd = ret; + if (fd < 0) + return NULL; if (ftruncate (fd, size) < 0) { -- 2.30.2